﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzyGoEzySystem.Interfaces;
using EzyGoEzySystem.Services;
using EzyGoEzySystem.Models;

namespace EzyGoEzySystem.Controllers
{
    public class TicketController : Controller
    {
        //
        // GET: /Ticket/

        EzyGoEzyDBContext dbContext = new EzyGoEzyDBContext();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Board(string phoneIds)
        {
            // initialize db context
            dbContext = new EzyGoEzyDBContext();

            ILoginService loginSvc = new LoginService();
            string[] phoneList = phoneIds.Split(',');
            IEnumerable<Account> accounts = loginSvc.FetchAccount(phoneList, dbContext);

            IBoardAlightService baSvc = new BoardAlightService();
            IEnumerable<TravelJournalEntry> journalEntries = baSvc.BoardBus(accounts, DateTime.Now, dbContext);

            IEzyNotification notificationSvc = new EzyNotificationService();

            foreach (Account account in accounts)
            {
                if (account.Balance <= 0)
                {
                    if (string.IsNullOrEmpty(account.CreditCard))
                    {
                        string message = String.Format("Hi, {0}. You have insufficient amount in your balance: {1}. Please top up!", account.Name, account.Balance.ToString());
                        notificationSvc.SendSMS(account.PhoneNumber, message);

                        string email = String.Format("Account ID: {0}, Phone No: {1}, Balance: {2}", account.ID, account.PhoneNumber, account.Balance);
                        notificationSvc.SendEmail("ezygoezyservice@gmail.com", "EzyGoEzy Service", "ezygoezyservice@gmail.com", "EzyGoEzy Director", "Account Balance Insufficience", email);
                    }
                }
                else
                {
                    string message = String.Format("Hi,{0}. Welcome onboard! {1}. Balance: {2}", account.Name, DateTime.Now, account.Balance.ToString());
                    notificationSvc.SendSMS(account.PhoneNumber, message);
                }
            }

            // cleanup dbContext
            dbContext.Dispose();

            return Content(String.Format("Boarded!"));
        }

        public ActionResult Alight(string phoneIds)
        {
            // initialize db context
            dbContext = new EzyGoEzyDBContext();

            ILoginService loginSvc = new LoginService();
            string[] phoneList = phoneIds.Split(',');
            IEnumerable<Account> accounts = loginSvc.FetchAccount(phoneList, dbContext);

            IBoardAlightService baSvc = new BoardAlightService();
            IEnumerable<TravelJournalEntry> journalEntries = baSvc.AlightBus(accounts, DateTime.Now, dbContext);

            IFarePayment paymentSvc = new FareManagementServices();
            paymentSvc.ChargeCalculate(journalEntries, dbContext);

            IEzyNotification notificationSvc = new EzyNotificationService();

            foreach (Account account in accounts)
            {
                if (account.Balance <= 0)
                {
                    if (string.IsNullOrEmpty(account.CreditCard))
                    {
                        string message = String.Format("Hi,{0}. You have insufficient amount in your balance: {1}. Please top up!", account.Name, account.Balance.ToString());
                        notificationSvc.SendSMS(account.PhoneNumber, message);

                        string email = String.Format("Account ID: {0}, Phone No: {1}, Balance: {2}", account.ID, account.PhoneNumber, account.Balance);
                        notificationSvc.SendEmail("ezygoezyservice@gmail.com", "EzyGoEzy Service", "ezygoezyservice@gmail.com", "EzyGoEzy Director", "Account Balance Insufficience", email);
                    }
                }
                else
                {
                    string message = String.Format("Good bye, {0}! Have a nice day! Alighted at: {1}. Balance: {2}", account.Name, DateTime.Now, account.Balance.ToString());
                    notificationSvc.SendSMS(account.PhoneNumber, message);
                }
            }

            // cleanup dbContext
            dbContext.Dispose();

            return Content(String.Format("Alighted!"));
        }
    }
}
